import "package:flutter/material.dart";
import "../widget/useDialog.dart";

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});
  @override
  State<DialogPage> createState() => DialogPageState();
}

class DialogPageState extends State<DialogPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('弹窗'),
      ),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
                onPressed: () async {
                  // result 可接受到 pop()传入的参数
                  var result = await showDialog(
                      barrierDismissible: true, //表示点击蒙版关闭弹窗
                      context: context,
                      builder: (context) {
                        return AlertDialog(
                          title: const Text('提示标题'),
                          content: const Text('提示内容'),
                          actions: [
                            TextButton(
                                onPressed: () {
                                  print('确认');
                                  Navigator.of(context).pop('ok'); //关闭当前弹窗
                                },
                                child: const Text('确认')),
                            TextButton(
                                onPressed: () {
                                  print('取消');
                                  Navigator.of(context).pop('cancel'); //关闭当前弹窗
                                },
                                child: const Text('取消'))
                          ],
                        );
                      });
                  print('result:' + result);
                },
                child: const Text('弹窗')),
            ElevatedButton(
                onPressed: () async {
                  var result = await showDialog(
                      barrierDismissible: true, //表示点击蒙版关闭弹窗
                      context: context,
                      builder: (context) {
                        return SimpleDialog(
                          title: const Text("请选择语言"),
                          children: [
                            SimpleDialogOption(
                              onPressed: () {
                                print('选择的是汉语');
                                Navigator.of(context).pop('汉语'); //关闭当前弹窗
                              },
                              child: const Text('汉语'),
                            ),
                            const Divider(),
                            SimpleDialogOption(
                              onPressed: () {
                                print('选择的是英语');
                                Navigator.of(context).pop('英语'); //关闭当前弹窗
                              },
                              child: const Text('英语'),
                            ),
                            const Divider(),
                            SimpleDialogOption(
                              onPressed: () {
                                print('选择的是日语');
                                Navigator.of(context).pop('日语'); //关闭当前弹窗
                              },
                              child: const Text('日语'),
                            ),
                          ],
                        );
                      });
                  print('result:' + result);
                },
                child: const Text('菜单选择弹窗')),
            ElevatedButton(
                onPressed: () async {
                  var result = await showModalBottomSheet(
                      context: context,
                      builder: (context) {
                        return SizedBox(
                          child: Column(
                            mainAxisSize: MainAxisSize.min, //自适应高度
                            children: [
                              ListTile(
                                title: const Text('取消关注'),
                                onTap: () {
                                  Navigator.of(context).pop('取消关注');
                                },
                              ),
                              ListTile(
                                title: const Text('举报'),
                                onTap: () {
                                  Navigator.of(context).pop('举报');
                                },
                              ),
                              ListTile(
                                title: const Text('投诉'),
                                onTap: () {
                                  Navigator.of(context).pop('投诉');
                                },
                              )
                            ],
                          ),
                        );
                      });
                  print('result:' + result);
                },
                child: const Text('底部弹出框')),
            ElevatedButton(
                onPressed: () async {
                  var result = await showDialog(
                      barrierDismissible: false,
                      context: context,
                      builder: (context) {
                        return UseDialog(
                          title: '提示',
                          content: '是否确认取消关注？',
                          onEnter: () {
                            Navigator.of(context).pop('确认');
                          },
                          onClose: () {
                            Navigator.of(context).pop('关闭');
                          },
                          onCancel: () {
                            Navigator.of(context).pop('取消');
                          },
                        );
                      });
                  print('result:' + result);
                },
                child: const Text('自定义dialog'))
          ],
        ),
      ),
    );
  }
}
